package ua.races.command.implementation;

import org.apache.log4j.Logger;
import ua.races.command.ServletCommand;
import ua.races.dao.RacesDaoImpl;
import ua.races.vo.Race;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class PrepareBookmakerCommand implements ServletCommand {
    private static final Logger log = Logger.getLogger(PrepareBookmakerCommand.class.getName());

    public void execute(HttpServletRequest req, HttpServletResponse resp, ServletContext context) {

        List<Race> races = null;
        RacesDaoImpl dao;

        dao = (RacesDaoImpl) context.getAttribute("DAO");

        try {
            races = dao.findAllRacesForBookmaker();
            req.setAttribute("races", races);
            log.info("races for bookmaker.jsp are successfully prepared");
        } catch (SQLException e) {
            log.error(e);
        }


        try {
            context.getRequestDispatcher("/bookmaker.jsp").forward(req, resp);
        } catch (ServletException e) {
            log.error(e);
        } catch (IOException e) {
            log.error(e);
        }

    }
}
